CC = g++

OBJC = objcopy
RM = rm -f

OPTIMIZE = -O3
CCFLAGS =  -g
SDL_CFLAGS := $(shell sdl-config --cflags) 
SDL_LDFLAGS := $(shell sdl-config --libs) 
LINKFLAGS = -T pc.ld -Wl,-Map=memory.map
PROFFLAGS = -pg

GAMEOBJDIR = ./objects
GAMEOBJS = sonic.o $(GAMEOBJDIR)/hud.o $(GAMEOBJDIR)/rings.o $(GAMEOBJDIR)/points.o $(GAMEOBJDIR)/monitor.o $(GAMEOBJDIR)/explosions.o $(GAMEOBJDIR)/animals.o $(GAMEOBJDIR)/springs.o $(GAMEOBJDIR)/spikes.o $(GAMEOBJDIR)/platforms.o $(GAMEOBJDIR)/titlecard.o $(GAMEOBJDIR)/gotthroughcard.o $(GAMEOBJDIR)/signpost.o $(GAMEOBJDIR)/scenery.o $(GAMEOBJDIR)/GHZ/bridge.o $(GAMEOBJDIR)/GHZ/motobug.o $(GAMEOBJDIR)/GHZ/chopper.o $(GAMEOBJDIR)/GHZ/buzzbomber.o $(GAMEOBJDIR)/GHZ/missile.o $(GAMEOBJDIR)/GHZ/newtron.o $(GAMEOBJDIR)/GHZ/crabmeat.o $(GAMEOBJDIR)/GHZ/purplerock.o $(GAMEOBJDIR)/GHZ/edgewalls.o $(GAMEOBJDIR)/GHZ/ledge.o $(GAMEOBJDIR)/GHZ/smash.o
OBJS = paletteNEWER.o anim.o camera.o collision.o decompress.o draw.o debug.o hud.o joypad.o mapping.o misc.o object.o objectsystemNEWER.o plcsystem.o plc.o sprite.o video.o videoext.o zoneDIFFSTRUCT.o mainsonic.o $(GAMEOBJS)

ARTUNC = artunc_sonic.o artunc_ghzlargeflower.o artunc_ghzsmallflower.o artunc_ghzwaterfall.o artunc_hudnumbers.o artunc_livesctrnums.o
BINOBJS = $(ARTUNC) collide_anglemap.o collide_arraynrm.o collide_arrayrot.o collide_GHZ.o collide_LZ.o collide_MZ.o collide_SLZ.o collide_SYZ.o collide_SBZ.o map16_GHZ.o map256_GHZ.o pal_SegaBG.o pal_Title.o pal_LevelSel.o pal_Sonic.o pal_GHZ.o pal_GHZ_cyc.o level_GHZ1.o level_GHZ2.o level_GHZ3.o level_GHZbg.o objpos_GHZ1.o objpos_GHZ2.o objpos_GHZ3.o startpos_GHZ1.o startpos_GHZ2.o startpos_GHZ3.o angles.o ghzbend1.o ghzbend2.o ghzledgemap.o baselines.o levelorder.o
BINDATA = ./bin

NEMDIR = $(BINDATA)/artnem
NEMOBJS = $(NEMDIR)/Title_Cards.o $(NEMDIR)/Signpost.o $(NEMDIR)/Hidden_Bonuses.o $(NEMDIR)/Giant_Ring_Flash.o $(NEMDIR)/Animal_Flicky.o $(NEMDIR)/Animal_Rabbit.o $(NEMDIR)/Enemy_Chopper.o $(NEMDIR)/Enemy_Newtron.o $(NEMDIR)/EnemyMotobug.o $(NEMDIR)/Spikes.o $(NEMDIR)/SpringHorizontal.o $(NEMDIR)/SpringVertical.o $(NEMDIR)/GHZ_Swinging_Platform.o $(NEMDIR)/GHZ_Bridge.o $(NEMDIR)/GHZ_Spiked_Log.o $(NEMDIR)/GHZ_Giant_Ball.o $(NEMDIR)/GHZ_Breakable_Wall.o $(NEMDIR)/GHZ_Edge_Wall.o 

	
sonic: $(OBJS) $(BINOBJS) $(NEMOBJS)
	$(CC) $(CCFLAGS) $(LINKFLAGS) $(OBJS) $(BINOBJS) $(NEMOBJS) $(SDL_LDFLAGS) $(OPTIMIZE) -o sonic

prof: $(OBJS) $(BINOBJS) $(NEMOBJS)
	$(CC) $(CCFLAGS) $(LINKFLAGS) $(OBJS) $(BINOBJS) $(NEMOBJS) $(SDL_LDFLAGS) -o sonic $(PROFFLAGS)


$(BINDATA)/artnem/%.o : $(BINDATA)/artnem/%.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


artunc_sonic.o: $(BINDATA)/artunc/Sonic.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

artunc_ghzwaterfall.o: $(BINDATA)/artunc/GHZ_Waterfall.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

artunc_ghzlargeflower.o : $(BINDATA)/artunc/GHZ_Flower_Large.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

artunc_ghzsmallflower.o : $(BINDATA)/artunc/GHZ_Flower_Small.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

artunc_hudnumbers.o : $(BINDATA)/artunc/HUD_Numbers.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

artunc_livesctrnums.o : $(BINDATA)/artunc/Lives_Counter_Numbers.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


collide_anglemap.o: $(BINDATA)/collide/Angle_Map.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

collide_arraynrm.o: $(BINDATA)/collide/Collision_Array_Normal.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

collide_arrayrot.o: $(BINDATA)/collide/Collision_Array_Rotated.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


collide_GHZ.o : $(BINDATA)/collide/GHZ.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

collide_LZ.o : $(BINDATA)/collide/LZ.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

collide_MZ.o : $(BINDATA)/collide/MZ.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

collide_SLZ.o : $(BINDATA)/collide/SLZ.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

collide_SYZ.o : $(BINDATA)/collide/SYZ.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

collide_SBZ.o : $(BINDATA)/collide/SBZ.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


map256_GHZ.o : $(BINDATA)/map256/GHZ.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


map16_GHZ.o : $(BINDATA)/map16/GHZ.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


pal_SegaBG.o : $(BINDATA)/palette/Sega_Background.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

pal_Title.o : $(BINDATA)/palette/Title_Screen.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

pal_LevelSel.o : $(BINDATA)/palette/Level_Select.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

pal_Sonic.o : $(BINDATA)/palette/Sonic.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

pal_GHZ.o : $(BINDATA)/palette/Green_Hill_Zone.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


pal_GHZ_cyc.o : $(BINDATA)/palette/Cycle_-_GHZ.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


level_GHZ1.o : $(BINDATA)/levels/ghz1.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

level_GHZ2.o : $(BINDATA)/levels/ghz2.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

level_GHZ3.o : $(BINDATA)/levels/ghz3.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

level_GHZbg.o : $(BINDATA)/levels/ghzbg.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


objpos_GHZ1.o : $(BINDATA)/objpos/ghz1.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

objpos_GHZ2.o : $(BINDATA)/objpos/ghz2.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

objpos_GHZ3.o : $(BINDATA)/objpos/ghz3.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


startpos_GHZ1.o : $(BINDATA)/startpos/ghz1.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

startpos_GHZ2.o : $(BINDATA)/startpos/ghz2.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

startpos_GHZ3.o : $(BINDATA)/startpos/ghz3.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


angles.o : $(BINDATA)/misc/angles.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


ghzbend1.o : $(BINDATA)/misc/ghzbend1.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

ghzbend2.o : $(BINDATA)/misc/ghzbend2.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

ghzledgemap.o : $(BINDATA)/misc/GHZ_Collapsing_Ledge_Heightmap.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

baselines.o : $(BINDATA)/misc/Oscillating_Number_Baselines.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@

levelorder.o : $(BINDATA)/misc/Level_Order.bin
	$(OBJC) -I binary -O elf32-i386 -B i386 $< $@


%.o: %.c
	$(CC) $(CCFLAGS) $(SDL_CFLAGS) -c $< $(OPTIMIZE) -o $@ 

clean:
	$(RM) *.o ./objects/*.o ./objects/GHZ/*.o
